package com.SoapClasses;


import java.io.IOException;
import java.net.SocketTimeoutException;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import com.Data.Profile;
import com.Data.SocialInfo;
import com.Data.User;
import com.Managers.LogManagerClass;
import com.Managers.UserManagerClass;


public class SoapUser {

    private static final String TAG = "SOAP_USER";

    SoapSerializationEnvelope envelope;

    //
    PropertyInfo pi;

    SoapObject Request;

    public String addUser(String[] data) {

        String METHOD_NAME = "addUser";
        String SOAP_ACTION = "http://ShareUp.com/addUser";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        //
        PropertyInfo pi = new PropertyInfo();
        pi = new PropertyInfo();
        pi.setName("data");
        pi.setValue(data);
        pi.setType(String.class);
        //
        String info = "";
        for (int i = 0; i < data.length; i++) {
            info += data[i] + "#";
        }
        Request.addProperty("data", info);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return "-10";
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String Trace = " ";
            for (StackTraceElement line : stack) {
                Trace += line.toString();
            }
            Log.e(TAG + ": ", Trace);
            LogManagerClass.writeLog(TAG + ": " + Trace);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return "-10";
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        //
        return response.toString();
    }

    public String deletePictureCover(int id, String cover) {

        String METHOD_NAME = "deletePictureCover";
        String SOAP_ACTION = "http://ShareUp.com/deletePictureCover";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);

        Request.addProperty("id", id);
        Request.addProperty("cover", cover);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return "-10";
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String Trace = " ";
            for (StackTraceElement line : stack) {
                Trace += line.toString();
            }
            Log.e(TAG + ": ", Trace);
            LogManagerClass.writeLog(TAG + ": " + Trace);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return "-10";
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }

        return response.toString();
    }

    public User getInfo(int IDUSER) {

        //
        String METHOD_NAME = "getUserInfo";
        String SOAP_ACTION = "http://ShareUp.com/getUserInfo";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        Request.addProperty("id", IDUSER);
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);

        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        /*
         * Call the web service and retrieve result ...
         */
        Object response = null;
        //
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
            //
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", "TimeOut");
            LogManagerClass.writeLog(TAG + ": " + "TimeOut");
            // ERROR -10 CONNECTION TIMEOUT
            User uTimeOut = new User();
            uTimeOut.setID(-10);
            return uTimeOut;
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String Trace = " ";
            for (StackTraceElement line : stack) {
                Trace += line.toString();
            }
            Log.e(TAG + ": ", Trace);
            LogManagerClass.writeLog(TAG + ": " + Trace);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            // ERROR -10 CONNECTION TIMEOUT
            User uTimeOut = new User();
            uTimeOut.setID(-10);
            return uTimeOut;
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
        }
        //
        //
        return getUserInfo(response);
    }

    public Profile getProfileInfo(int id) {

        //
        String METHOD_NAME = "getUserProfileInfo";
        String SOAP_ACTION = "http://ShareUp.com/getUserProfileInfo";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        Request.addProperty("id", id);
        /*
         * Set the web service envelope
         */
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        envelope.addMapping(SoapUtil.NAMESPACE, "User", new User().getClass());
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        /*
         * Call the web service and retrieve result ...
         */
        Object response = null;
        //
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
            //
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            Profile p = new Profile();
            p.setSex(-10);
            return p;
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String Trace = " ";
            for (StackTraceElement line : stack) {
                Trace += line.toString();
            }
            Log.e(TAG + ": ", Trace);
            LogManagerClass.writeLog(TAG + ": " + Trace);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            Profile p = new Profile();
            p.setSex(-10);
            return p;
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
        }
        //
        //
        return getUserProfileInfo(response);
    }

    public SocialInfo getUserSocialInfo(int id) {

        String METHOD_NAME = "getSocialInfo";
        String SOAP_ACTION = "http://ShareUp.com/getSocialInfo";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        Request.addProperty("id", id);
        Request.addProperty("privacy", 0);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            SocialInfo si = new SocialInfo();
            si.setIDUser(-10);
            return si;
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String Trace = " ";
            for (StackTraceElement line : stack) {
                Trace += line.toString();
            }
            Log.e(TAG + ": ", Trace);
            LogManagerClass.writeLog(TAG + ": " + Trace);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());

            SocialInfo si = new SocialInfo();
            si.setIDUser(-10);
            return si;
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
        }
        //
        return getUserSocialInfo(response);
    }

    public User login(String user, String password) {

        //
        String METHOD_NAME = "getLogin";
        String SOAP_ACTION = "http://ShareUp.com/getLogin";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        //
        Request.addProperty("user", user);
        Request.addProperty("pass", password);
        /*
         * Set the web service envelope
         */
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);

        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);

        /*
         * Call the web service and retrieve result ...
         */
        Object response = null;
        //
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
            //
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            // ERROR -10 CONNECTION TIMEOUT
            User uTimeOut = new User();
            uTimeOut.setID(-10);
            return uTimeOut;
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String Trace = " ";
            for (StackTraceElement line : stack) {
                Trace += line.toString();
            }
            Log.e(TAG + ": ", Trace);
            LogManagerClass.writeLog(TAG + ": " + Trace);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            // ERROR -10 CONNECTION TIMEOUT
            User uTimeOut = new User();
            uTimeOut.setID(-10);
            return uTimeOut;
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
        }
        //
        //
        return getUserInfo(response);
    }

    public String updateProfile(String[] data) {

        String METHOD_NAME = "updateProfile";
        String SOAP_ACTION = "http://ShareUp.com/updateProfile";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        //
        String info = "";
        for (int i = 0; i < data.length; i++) {
            info += data[i] + "#";
        }
        Request.addProperty("data", info);
        //
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return "-10";
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String Trace = " ";
            for (StackTraceElement line : stack) {
                Trace += line.toString();
            }
            Log.e(TAG + ": ", Trace);
            LogManagerClass.writeLog(TAG + ": " + Trace);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return "-10";
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        //
        return response.toString();
    }

    public String updateState(int state) {

        String METHOD_NAME = "updateState";
        String SOAP_ACTION = "http://ShareUp.com/updateState";
        //
        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);

        Request.addProperty("id", UserManagerClass.UserLogged.getID());
        Request.addProperty("state", state);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);
        //
        Object response = null;
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();
        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", "TIMEOUT");
            LogManagerClass.writeLog(TAG + ": " + "TIMEOUT");
            return "-10";
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String Trace = " ";
            for (StackTraceElement line : stack) {
                Trace += line.toString();
            }
            Log.e(TAG + ": ", Trace);
            LogManagerClass.writeLog(TAG + ": " + Trace);
        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return "-10";
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());
            return x.getMessage();
        }
        //
        return response.toString();
    }

    private User getUserInfo(Object sp) {

        String RES = sp.toString();

        try {
            JSONObject jo = new JSONObject(RES);
            User us = new User();
            int ID = Integer.parseInt(jo.getString("ID"));
            String RealName = jo.getString("RealName");
            String Email = jo.getString("Email");
            // String Phone = jo.getString("Phone");
            String ProfilePicture = jo.getString("ProfilePicture");
            String CoverPicture = jo.getString("CoverPicture");
            int Sex = Integer.parseInt(jo.getString("Sex"));
            String Description = jo.getString("Description");
            int Follow = Integer.parseInt(jo.getString("Follow"));
            int Privacy = jo.getInt("Privacy");
            int Blur = jo.getInt("Blur");
            //
            us.setID(ID);
            us.setEmail(Email);
            us.setIsFollowingMe(Follow);
            us.setRealName(RealName);
            us.setSex(Sex);
            us.setProfilePicture(ProfilePicture);
            us.setCoverPicture(CoverPicture);
            us.setDescription(Description);
            us.setPrivacy(Privacy);
            us.setBlur(Blur);

            return us;
        }
        catch (JSONException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());

            return null;
        }

    }

    private Profile getUserProfileInfo(Object sp) {

        String RES = sp.toString();

        try {
            JSONObject jo = new JSONObject(RES);
            Profile pro = new Profile();

            String RealName = jo.getString("RealName");
            int Sex = Integer.parseInt(jo.getString("Sex"));
            String Email = jo.getString("Email");
            String Description = jo.getString("Description");
            String Picture = jo.getString("Picture");
            //
            pro.setDescription(Description);
            pro.setEmail(Email);
            pro.setPicture(Picture);
            pro.setRealName(RealName);
            pro.setSex(Sex);

            return pro;
        }
        catch (JSONException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());

            return null;
        }
    }

    private SocialInfo getUserSocialInfo(Object sp) {

        String RES = sp.toString();

        try {
            JSONObject jo = new JSONObject(RES);
            SocialInfo si = new SocialInfo();

            int ID = Integer.parseInt(jo.getString("IDUser"));
            int Followers = Integer.parseInt(jo.getString("Followers"));
            int Following = Integer.parseInt(jo.getString("Following"));
            int Pictures = Integer.parseInt(jo.getString("Pictures"));
            String Description = jo.getString("Description");
            Boolean ShowPrivate = jo.getBoolean("ShowPrivate");
            String AdditionalData = jo.getString("AdditionalData");
            String LastSeen = jo.getString("LastSeen");

            //
            si.setAditionalData(AdditionalData);
            si.setDescription(Description);
            si.setFollowers(Followers);
            si.setFollowing(Following);
            si.setLastSeen(LastSeen);
            si.setIDUser(ID);
            si.setPictures(Pictures);
            si.setShowPrivate(ShowPrivate);

            return si;
        }
        catch (JSONException x) {
            Log.e(TAG + ": ", x.getMessage());
            LogManagerClass.writeLog(TAG + ": " + x.getMessage());

            return null;
        }
    }
}
